
import moment from 'moment'

function timeAgo(date) {
  const now = new Date()
  const diffInSeconds = Math.floor((now - new Date(date)) / 1000) // 计算时间差（秒）

  const minute = 60
  const hour = 60 * minute
  const day = 24 * hour
  const week = 7 * day

  if (diffInSeconds < minute) {
    return `${diffInSeconds}秒前`
  } else if (diffInSeconds < hour) {
    return `${Math.floor(diffInSeconds / minute)}分钟前`
  } else if (diffInSeconds < day) {
    return `${Math.floor(diffInSeconds / hour)}小时前`
  } else if (diffInSeconds < week) {
    return `${Math.floor(diffInSeconds / day)}天前`
  } else {
    return `${Math.floor(diffInSeconds / week)}周前`
  }
}

function format(time, format = 'YYYY-MM-DD') {
  return moment(time).format(format)
}

export {
  timeAgo,
  format
}